<?php

namespace app\index\service;

use think\Db;

/**
 * @author xjy
 */
class ProduceService
{
    private static $salesState=["1"=>"新产品","2"=>"清仓品","3"=>"在售品","4"=>"下架品"];  //销售状态
    private static $cycleStage=["1"=>"补货期","2"=>"新品期","3"=>"计划淘汰期","4"=>"淘汰期"];  //生命周期

    //SKU生命周期列表
    static public function getSkuLifeCycleList($param)
    {
        if(empty($param['down'])){
             $titles=[];
        }else{
             $titles=[
                 'sku'=>'SKU','cname'=>'中文名称','display_classified'=>'陈列类','classified_one'=>'大类','classified_two'=>'中类','classified_three'=>'小类','classified_four'=>'细类',
                 'sales_state_name'=>'销售状态','cycle_stage_name'=>'生命周期','sku_grade'=>'SKU等级','is_time_name'=>'季节性产品','seasonal_cycle'=>'季节月份','state'=>'状态','operator'=>'修改人','update_time'=>'修改时间'];
        }
        
        return \app\index\model\ProduceModel::getSkuLifeCycleList($param,$titles,"app\index\service\ProduceService::convertSkuCycleData");  
    }
    public static function convertSkuCycleData(){
       
        $gets=func_get_args();
        if($gets[0]){
            foreach($gets[0] as $key=>&$value){
                $value['sales_state_name']=isset(self::$salesState[$value['sales_state']]) ? self::$salesState[$value['sales_state']]:'';
                $value['cycle_stage_name']=isset(self::$cycleStage[$value['cycle_stage']])? self::$cycleStage[$value['cycle_stage']]:'';
                $value['state']=($value['status']==1) ? "正常":"禁止";
                $value['seasonal_cycle']="'".$value['seasonal_cycle'];
                $value['is_time_name']=($value['is_time']==1) ? "是":"否";
            }
        }
        return $gets[0];  
    }
     //校验批量处理
    static public function uploadHandle($data,$param){
        $msg=[];//提示信息
        $num=0;
        $sku=[];$insertData=[];
        
        $getCountry=\app\common\ConfigBase::getCountry();
        foreach ($data as $k=>$v){
            if(!empty($v['0'])){
                $sku[]=strtoupper($v[0]);
                $num++;
            } 
        }
        if($num>5000){
             return ["code" => "0", "msg" => '上传不能大于5000'];
        }
        $res=\app\common\service\SkuService::getBatchInfoBySku($sku);
        $skuName=array_column($res,'cnname','sku');
        $title=['SKU','display_classified','classified_one','classified_two','classified_three','classified_four','sales_state','cycle_stage','sku_grade','is_time','season_start_time','season_end_time'];
        foreach($data as $key=>$value){
            $one=[];$oneMsg=[];$seasonal_cycle='';
            $line=$key+2;
            $cname = isset($skuName[$value['0']]) ? $skuName[$value['0']]:'';
            if((!empty($value['0']) && empty($cname)) || empty($value['0'])){
                $oneMsg[]="第{$line}行SKU:{$value['0']}不存在或者sku不存在";
            }
            for($i=1;$i<10;$i++){
                if($value[$i]){
                  $one[$title[$i]]=$value[$i]; 
                }
            }
            if(empty($value['9'])){
                $one['is_time']=2; 
            }
            if(!empty($value['0'])){
                 $one['SKU'] = strtoupper($value['0']);
            }
            if(!empty($cname)){
                 $one['cname'] = $cname;
            }
            if(!empty($value['6']) && ($value['6']>4 || $value['6']<1)){
                 $oneMsg[]="第{$line}行:销售状态只能是1-4";
            }
            if(!empty($value['7']) &&  ($value['7']>4 || $value['7']<1)){  
                $oneMsg[]="第{$line}行:生命周期只能是1-4";
            }
         
            if(!empty($value['9']) && is_numeric($value['9']) && ($value['9']>0 && $value['9']<3)){
                $one['is_time'] = $value['9'];
            }else if(!empty($value['9']) && ($value['9']>2 || $value['9']<1)){
                $oneMsg[]="第{$line}行:是否是季节性产品不能为空而且只能是1-2";
            }
            if($one['is_time']==1){
                if(!empty($value['10']) && is_numeric($value['10']) && ($value['10']>0 && $value['10']<13)){
                    $seasonal_cycle .= $value['10']; 
                }else{
                    $oneMsg[]="第{$line}行:季节开始月份不能为空只能是1-12";
                }
                if(!empty($value['11']) && is_numeric($value['11']) && ($value['11']>0 && $value['11']<13)){
                    $seasonal_cycle .= "-".$value['11'];  
                }else{
                    $oneMsg[]="第{$line}行:季节结束月份不能为空只能是1-12";
                }
                 if($value['10']>=$value['11']){
                     $oneMsg[]="第{$line}行:季节开始月份只能小于季节结束月份";
                 }
            }
 
            if($one['is_time']==1 && !empty($seasonal_cycle)){
                $one['seasonal_cycle'] = $seasonal_cycle;
            }else{
                $one['seasonal_cycle'] = '';
            }
            $one['update_time'] = date('Y-m-d H:i:s');
            $one['operator'] = CN_NAME;
            if(count($oneMsg)==0){
                $insertData[]=$one;
            }else{
                $msg[]=join('<br/>',$oneMsg);
            }
        }
        \app\common\model\ToolModel::dbInsertOrUpdate('sku_life_cycle',$insertData);
        return ["code" => "1", "msg" => join('<br/>',$msg)];
    }
}
